﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PoSHEditor
{
    public partial class FunctionProvider : UserControl
    {
        public FunctionProvider()
        {
            InitializeComponent();
            DisplayFunctionNames();
        }

        int _functionCount;
        public void DisplayFunctionNames()
        {
            Singletons.TheScriptEngine.AddOutString(false);
            InvokeScriptResults res = Singletons.TheScriptEngine.InvokeScriptWithResults("(dir function:).count");
            int fc = Convert.ToInt32(res.PSObjects[0].BaseObject);
            
            if (_functionCount < fc)
            {
                _functionCount = fc;

                Singletons.TheScriptEngine.AddOutString(false);
                res = Singletons.TheScriptEngine.InvokeScriptWithResults("dir function: | sort");

                this.lstFunctionNames.DataSource = res.PSObjects;
            }
        }

        private void lstFunctionNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetFunctionContents();
        }

        public void GetFunctionContents()
        {
            Singletons.TheScriptEngine.AddOutString(false);
            InvokeScriptResults res = Singletons
                .TheScriptEngine
                .InvokeScriptWithResults(@"get-content function:\" + lstFunctionNames.Text);

            txtFunctionContent.Text = res.PSObjects[0].BaseObject.ToString();
        }
    }
}
